package com.eastfair.pay.service.impl;

import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.cache.Cache;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.boot.request.PageParams;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import com.eastfair.pay.dao.MerchantSystemMapper;
import com.eastfair.pay.dto.MerchantSystemDTO;
import com.eastfair.pay.dto.MerchantSystemPageQuery;
import com.eastfair.pay.entity.MerchantSystem;
import com.eastfair.pay.service.MerchantSystemService;
import com.eastfair.pay.vo.MerchantSystemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* <p>
    * 业务实现类
    * 商户系统
    * </p>
*
* @author clm
* @date 2023-02-22
*/
@Slf4j
@Service
//@DS("#thread.tenant")
    public class MerchantSystemServiceImpl extends SuperServiceImpl<MerchantSystemMapper, MerchantSystem> implements MerchantSystemService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<MerchantSystem> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<MerchantSystem> list(MerchantSystemDTO modelDTO, List<Long> ids) {
        return ((MerchantSystemService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       "{'isEnabled', #args[0]?.isEnabled},"+
       "{'systemName', #args[0]?.systemName},"+
       "{'createrName', #args[0]?.createrName},"+
       "{'bindMerchant', #args[0]?.bindMerchant},"+
       "{'bindMerchantId', #args[0]?.bindMerchantId},"+
       "{'validStartTime', #args[0]?.validStartTime},"+
       "{'validEndTime', #args[0]?.validEndTime},"+
       "{'validType', #args[0]?.validType},"+
       "{'systemCode', #args[0]?.systemCode},"+
        "{'id', #args[1]}"+
        "}"))
    public List<MerchantSystem> listOfCache(MerchantSystemDTO modelDTO, List<Long> ids) {
       //查询
        MerchantSystem queryModel = new MerchantSystem();
        if (modelDTO != null) {
            BeanUtils.copyProperties(modelDTO,queryModel);
        }
        QueryWrap<MerchantSystem> wrapper = Wraps.q(queryModel);
        if (ids != null && !ids.isEmpty()) {
            wrapper.lambda().in(MerchantSystem::getId, ids);
        }
        //添加其他业务查询条件
        wrapper.lambda().eq(MerchantSystem::getIsDeleted, BusinessConstant.DELETE_NO);
        List<MerchantSystem> modelList = list(wrapper);
        return modelList;
    }


    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(MerchantSystem model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        return R.successDef();
    }

    @Override
    public MerchantSystem getByBindMerchantId(Long merchantId) {
        LambdaQueryWrapper<MerchantSystem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MerchantSystem::getIsDeleted, BusinessConstant.DELETE_NO);
        lambdaQueryWrapper.eq(MerchantSystem::getId, merchantId);
        return baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public MerchantSystem getBySystemCode(String systemCode) {
        LambdaQueryWrapper<MerchantSystem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MerchantSystem::getIsDeleted, BusinessConstant.DELETE_NO);
        lambdaQueryWrapper.eq(MerchantSystem::getSystemCode, systemCode);
        return baseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * @param params
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.eastfair.pay.vo.MerchantSystemVO>
     * @Author clm
     * @Description //分页查询
     * @Date 15:08 2023/2/22
     * @Param [params]
     */
    @Override
    public Page<MerchantSystemVO> queryList(PageParams<MerchantSystemPageQuery> params) {
        MerchantSystemPageQuery model = params.getModel();

        LambdaQueryWrapper<MerchantSystem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotBlank(model.getBindMerchant()), MerchantSystem::getBindMerchant, model.getBindMerchant());
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(model.getSystemCode()), MerchantSystem::getSystemCode, model.getSystemCode());
        lambdaQueryWrapper.between(model.getValidStartTime() != null && model.getValidEndTime() != null,
                MerchantSystem::getCreateTime, model.getValidStartTime(), model.getValidEndTime());
        lambdaQueryWrapper.eq(MerchantSystem::getIsDeleted, BusinessConstant.DELETE_NO);
        //参数转化
        Page<MerchantSystem> merchantSystemPage = params.buildPage();
        //查询
        Page<MerchantSystem> merchantSystem = page(merchantSystemPage, lambdaQueryWrapper);
        //实体转化
        Page<MerchantSystemVO> merchantSystemVOPage = ConvertUtil.convertPage(merchantSystem, MerchantSystemVO.class);

        return merchantSystemVOPage;
    }

    @Override
    public void delete(MerchantSystem merchantSystem) {
        merchantSystem.setIsDeleted(BusinessConstant.YES);
        updateById(merchantSystem);
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public MerchantSystemVO doToVo(MerchantSystem model) {
        MerchantSystemVO modelVo = new MerchantSystemVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<MerchantSystemVO> doToVos(List<MerchantSystem> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<MerchantSystemVO> result = new ArrayList<>();
        for (MerchantSystem model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }
}
